package com.wxzz.elearing.comment.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.comment.dto.AnswersDescDto;
import com.wxzz.elearing.comment.dto.AnswersDto;
import com.wxzz.elearing.comment.dto.OrderIdDto;
import com.wxzz.elearing.comment.dto.QuestionsAnswersDto;
import com.wxzz.elearing.comment.entity.Comment;
import com.wxzz.elearing.comment.entity.QuestionsAnswersList;
import com.wxzz.elearing.comment.service.ICommentService;
import com.wxzz.elearing.comment.service.IQuestionsAnswersListService;
import com.wxzz.elearing.comment.vo.AnswersVo;
import com.wxzz.elearing.comment.vo.QuestionsContentDescVo;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wx-comment-controller")
@Api(tags = "问答评论接口(小程序)")
public class WxCommentController {

    @Autowired
    private ICommentService commentService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @ApiOperation(value = "问答评论(小程序)", notes = "问答评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveComment", method = RequestMethod.POST)
    public ReturnVO saveComment(@RequestBody Comment comment, @RequestHeader(value = "token") String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        comment.setIcon(jsonObject.getString("icon"));
        comment.setNickname(jsonObject.getString("nickname"));
        comment.setWxUserId(jsonObject.getLong("id"));
        return commentService.saveComment(comment);
    }

    @ApiOperation(value = "问答根据文章id获取详情(小程序)", notes = "问答根据文章id获取详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listAllComment/{articleId}", method = RequestMethod.POST)
    public ReturnVO<QuestionsContentDescVo> listAllComment(@PathVariable Long articleId) {
        return commentService.listAllComment(articleId);
    }

    @ApiOperation(value = "问答根据文章id列出所有评论(小程序)", notes = "问答根据文章id列出所有评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "answersDescList", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Comment>>> answersDescList(@RequestBody AnswersDescDto answersDescDto) {
        return commentService.answersDescList(answersDescDto);
    }

    @ApiOperation(value = "问答查询个人所有评论(小程序)", notes = "问答查询个人所有评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "answersList", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<AnswersVo>>> getAnswersList(@RequestBody AnswersDto answersDto) {
        return commentService.getAnswersList(answersDto);
    }

    @ApiOperation(value = "问答采纳评论(小程序)", notes = "问答采纳评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "setAccept", method = RequestMethod.POST)
    public ReturnVO setAccept(@RequestBody QuestionsAnswersDto questionsAnswersDto) {
        return commentService.setAccept(questionsAnswersDto);
    }


}
